<template>
  <div id="schoolMajorCascadeSelector">
    <el-cascader ref="cascader"
                 :props="props"
                 :options="options"
                 placeholder="请选择专业"
                 clearable
                 @change="handleChange"
    ></el-cascader>
  </div>
</template>
<script>
import { listSchool } from '@/api/curriculumSystemManagement/school'
import { listMajor } from '@/api/curriculumSystemManagement/major'

export default {
  model:{
    prop:'mid',
    event:'change'
  },
  props: {
    mid:{
      type:Array,
      default:()=>{
        return []
      }
    }
  },
  data() {
    return {
      resultSelection: [],
      props: {
        lazy: true,
        multiple: true,
        lazyLoad(node, resolve) {
          let { level, value, label } = node
          let postData = {
            pageNum: 1,
            pageSize: 1000
          }
          postData.sid = value
          listMajor(postData).then(res => {
            let oData = res.rows
            let options = []
            let item = {}
            for (let t of oData) {
              item = {}
              item.value = t.id
              item.label = t.mname
              item.leaf = true
              item.children = []
              options.push(item)
            }
            resolve(options)
          })
        }
      },
      options: [
        {
          id: 1,
          value: '1',
          label: '1',
          children: []
        }
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 1000
      }
    }
  },
  created() {
    this.getSchoolListolList()
  },
  methods: {
    //选择
    handleChange(e) {
      const checkNode = this.$refs.cascader.getCheckedNodes(true)
      let options = []
      for (let t of checkNode) {
        options.push(t.value)
      }
      this.$emit('change',options)
    },
    async getSchoolListolList() {
      const self = this
      const { rows } = await listSchool(self.queryParams)
      const afterData = self.structureTransformation(rows)
      self.options = afterData
    },
    //将学校或专业数据转化为树形
    structureTransformation(oData) {
      if (Array.isArray(oData)) {
        let options = []
        let item = {}
        for (let t of oData) {
          item = {}
          item.value = t.id
          item.label = t.sname
          item.leaf = false
          item.children = []
          options.push(item)
        }
        return options
      } else {
        console.log('数据不是数组,转化错误')
        return []
      }
    },
    //根据学校id获取专业数据
    async getMajorBySchoolId(sId) {
      const self = this
      let postData = JSON.parse(JSON.stringify(self.queryParams))
      postData.sId = sId
      const data = await listMajor(postData)
      console.log('专业数据', data)
    }
  }
}
</script>
<style scoped lang="scss">
#schoolMajorCascadeSelector {

}
</style>
